package com.coincalf.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.coincalf.admin.common.CommonUtils;
import com.coincalf.admin.entity.Account;
import com.coincalf.admin.entity.ForexAccount;
import com.coincalf.admin.entity.Market;
import com.coincalf.admin.entity.User;
import com.coincalf.admin.mapper.MarketMapper;
import com.coincalf.admin.service.ForexAccountService;
import com.coincalf.admin.service.MarketService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.coincalf.admin.service.UserService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 交易对配置信息 服务实现类
 * </p>
 *
 * @author qiang
 * @since 2018-05-13
 */
@Slf4j
@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> implements MarketService {
    private  static  final  int defaultSize=100;
    @Autowired
    ForexAccountService forexAccountService;
    @Autowired
    private UserService userService;
    @Override
    @Async
    public void ansyUpdateForexAccounts(List<ForexAccount> forexUserIds, Long marketKId, String marketName) {


        log.info("-----ansyUpdateForexAccounts--------"+forexUserIds+",========marketKId======"+marketKId+"----marketName--"+marketName);
        Integer [] ustyps={CommonUtils.USER_TYPE_0,CommonUtils.USER_TYPE_1};
        List<User>userList=userService.selectList( new EntityWrapper<User>().in("type",ustyps));

        if(userList!=null&&userList.size()>0) {


            List<List<User>> rsList = Lists.partition(userList, defaultSize);
            int i=0;
            for (List<User> users : rsList) {
                List<ForexAccount> Buyaccounts=new ArrayList<ForexAccount>();
                List<ForexAccount> Selleraccounts=new ArrayList<ForexAccount>();
                for (User us:users) {

                    ForexAccount BuyAccount=new ForexAccount();
                    BuyAccount.setUserId(us.getId());
                    BuyAccount.setStatus(CommonUtils.STATUS_1);
                    BuyAccount.setMarketId(marketKId);
                    BuyAccount.setMarketName(marketName);
                    BuyAccount.setAmount(new BigDecimal("0.00000000"));
                    BuyAccount.setLockAmount(new BigDecimal("0.00000000"));
                    BuyAccount.setType(CommonUtils.FORES_STATUS_1);
                    Buyaccounts.add(BuyAccount);

                    ForexAccount SellerAccount=new ForexAccount();
                    SellerAccount.setUserId(us.getId());
                    SellerAccount.setStatus(CommonUtils.STATUS_1);
                    SellerAccount.setMarketId(marketKId);
                    SellerAccount.setMarketName(marketName);
                    SellerAccount.setAmount(new BigDecimal("0.00000000"));
                    SellerAccount.setLockAmount(new BigDecimal("0.00000000"));
                    SellerAccount.setType(CommonUtils.FORES_STATUS_2);
                    Selleraccounts.add(SellerAccount);
                }
                log.info(i+"-----ansyUpdateAccounts- Buyaccounts---result---============"+Buyaccounts);
                log.info(i+"-----ansyUpdateAccounts- Selleraccounts---result---==========="+Selleraccounts);
                if(Buyaccounts!=null){
                    Boolean rs= forexAccountService.insertBatch(Buyaccounts);
                    log.info(i+"-----ansyUpdateAccounts- Buyaccounts---result---============"+rs);
                }

                if(Selleraccounts!=null){
                    Boolean rs= forexAccountService.insertBatch(Selleraccounts);
                    log.info(i+"-----ansyUpdateAccounts- Selleraccounts---result---============"+rs);
                }



            }
      /*      int n = userList.size() / defaultSize;
            int mt = userList.size() % defaultSize;
            if (mt > 0 || n <= 0) {
                n = n + 1;
            }
            //插入次数
            for (int i=0; i<n;i++) {
                int start = i * defaultSize;//0   100 200
                int end = start + defaultSize;//99 199
                List<User> newlist = userList.subList(start, end - 1);
                List<ForexAccount> Buyaccounts=new ArrayList<ForexAccount>();
                List<ForexAccount> Selleraccounts=new ArrayList<ForexAccount>();
                for (User us:newlist) {
                    ForexAccount BuyAccount=new ForexAccount();
                    BuyAccount.setUserId(us.getId());
                    BuyAccount.setStatus(CommonUtils.STATUS_1);
                    BuyAccount.setMarketId(marketKId);
                    BuyAccount.setMarketName(marketName);
                    BuyAccount.setAmount(new BigDecimal("0.00000000"));
                    BuyAccount.setLockAmount(new BigDecimal("0.00000000"));
                    BuyAccount.setType(CommonUtils.FORES_STATUS_1);
                    Buyaccounts.add(BuyAccount);

                    ForexAccount SellerAccount=new ForexAccount();
                    SellerAccount.setUserId(us.getId());
                    SellerAccount.setStatus(CommonUtils.STATUS_1);
                    SellerAccount.setMarketId(marketKId);
                    SellerAccount.setMarketName(marketName);
                    SellerAccount.setAmount(new BigDecimal("0.00000000"));
                    SellerAccount.setLockAmount(new BigDecimal("0.00000000"));
                    SellerAccount.setType(CommonUtils.FORES_STATUS_2);
                    Selleraccounts.add(SellerAccount);
                }
                log.info(i+"-----ansyUpdateAccounts- Buyaccounts---result---============"+Buyaccounts);
                log.info(i+"-----ansyUpdateAccounts- Selleraccounts---result---==========="+Selleraccounts);
                if(Buyaccounts!=null){
                    Boolean rs= forexAccountService.insertBatch(Buyaccounts);
                    log.info(i+"-----ansyUpdateAccounts- Buyaccounts---result---============"+rs);
                }

                if(Selleraccounts!=null){
                    Boolean rs= forexAccountService.insertBatch(Selleraccounts);
                    log.info(i+"-----ansyUpdateAccounts- Selleraccounts---result---============"+rs);
                }
            }*/
        }
    }
}
